package com.heba.adapter;

import android.content.Context;
import androidx.viewpager.widget.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.heba.bean.ItemGuideObject;
import com.heba.greenmulu.R;
import com.heba.utils.UniversalImageLoadTool;

import java.util.List;

/**
 * 引导页轮播图
 * <p>
 * Created by xingbao on 2018/4/11.
 */

public class GuidePagerAdapter extends PagerAdapter {

    private Context context;
    private List<ItemGuideObject> list;

    public GuidePagerAdapter(Context context, List<ItemGuideObject> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return Integer.MAX_VALUE;
    }

    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0 == arg1;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, final int position) {
        if (!list.isEmpty()) {
            final int park = position % list.size();
            View view = LayoutInflater.from(context).inflate(R.layout.item_guide01, null);
            ImageView image = view.findViewById(R.id.image);
            TextView txt_guide_title = view.findViewById(R.id.txt_guide_title);
            TextView txt_guide_content = view.findViewById(R.id.txt_guide_content);

            final ItemGuideObject object = list.get(park);
            txt_guide_title.setText(object.getGuideTitle());
            txt_guide_content.setText(object.getGuideContent());
            UniversalImageLoadTool.disPlay(object.getImgUrl(), image);
            view.setTag(position);
            container.addView(view, 0);

            return view;
        } else {
            return null;
        }
    }

}
